import router from "./index";
import store from "../store/index";
import { getToken, removeToken, removeUsername } from "@/utils/app";

const whiteRouter = ['/login'];

// 路由守卫
router.beforeEach((to, from, next) => {
    if (getToken()) {
        // 如果进入登录页则删除token和用户名
        if (to.path === "/login") {
            removeToken();
            removeUsername();
            store.commit("app/SET_TOKEN", "");
            store.commit("app/SET_USERNAME", "");
        }
        next();
    } else {
        if (whiteRouter.indexOf(to.path) !== -1) {
            next();
        } else {
            next("/login");
        }
    }
})